package com.lifp.consumer;

import com.lifp.anno.MqttService;
import com.lifp.anno.MqttTopic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;

/**
 * @author lifp
 * @date 2023/10/20
 */
@Slf4j
@MqttService
public class MqttConsumer {

    // 如果系统上下线主题已经在配置文件中有订阅了, 那么久不需要再自动订阅
    @MqttTopic(value = "$SYS/brokers/+/clients/+/connected",autoSubscribe = true)
    public void connected(Message<?> message){
        log.info("有什么东西连上了: {}",message.getPayload());
    }

    @MqttTopic(value ="$SYS/brokers/+/clients/+/disconnected",autoSubscribe = true)
    public void disconnected(Message<?> message){
        log.info("disconnected: {}",message.getPayload());
    }

    // 这里的 # 号是通配符 这里已经实现自动订阅了
    @MqttTopic(value = "test/#",autoSubscribe = false)
    public void test(Message<?> message){
        log.info("test="+message.getPayload());
    }

    // 这里的 + 号是通配符
    @MqttTopic(value = "topic/+/+/up",autoSubscribe = false)
    public void up(Message<?> message){
        log.info("topic: {}, payload: {}",message.getHeaders().get("mqtt_receivedTopic",String.class),message.getPayload());
    }

    // 这里就不自动订阅该主题了 ,
    @MqttTopic(value = "topic/1/2/down",autoSubscribe = true)
    public void down(Message<?> message){
        log.info("down="+message.getPayload());
    }
}
